package com.eastfair.projectcore.service.impl;

import com.eastfair.boot.service.SuperCacheServiceImpl;
import com.eastfair.cache.projectcore.FuncationCacheKeyBuilder;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.model.CacheKeyBuilder;
import com.eastfair.core.snowflake.DefaultUidGenerator;
import com.eastfair.projectcore.dao.FunctionMapper;
import com.eastfair.projectcore.entity.Function;
import com.eastfair.projectcore.service.FunctionService;
import com.eastfair.projectcore.vo.FunctionVO;
import com.eastfair.uid.model.SelfUidDto;
import com.eastfair.uid.service.SelfUidService;
import com.eastfair.util.CodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 功能
 * </p>
 *
 * @author ligang
 * @date 2021-07-26
 */
@Slf4j
@Service
public class FunctionServiceImpl extends SuperCacheServiceImpl<FunctionMapper, Function> implements FunctionService {

    //    @Autowired
//    SelfUidService selfUidService;
    private Function model;

    @Override
    protected CacheKeyBuilder cacheKeyBuilder() {
        return new FuncationCacheKeyBuilder();
    }

    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(Function model) {
        this.model = model;
        //雪花ID,不需要分库分表
//        SelfUidDto selfUidDto = new SelfUidDto();
//        model.setId(selfUidService.getUid(selfUidDto));
        model.setId(DefaultUidGenerator.getUid(0L));
        //code 码
        if (StringUtils.isBlank(model.getCode())) {
            //不用去重
            model.setCode(CodeUtils.randomCode(6));
        }
        return R.successDef();
    }

    @Override
    public R del(Long id) {
        Function function = getById(id);
        if (function != null) {
            function.setIsDeleted(BusinessConstant.DELETE_YES);
            updateById(function);
        }
        return R.successDef();
    }

    @Override
    public List<FunctionVO> findAll(List<Long> ids) {
        List<Function> functionList = new ArrayList<>();
        if (ids != null && !ids.isEmpty()) {
            functionList = listByIds(ids);
        } else {
            functionList = list();
        }
        return doToVos(functionList);
    }

    public FunctionVO doToVo(Function function) {
        FunctionVO functionVo = new FunctionVO();
        BeanUtils.copyProperties(function, functionVo);
        functionVo.setId(function.getId());
        return functionVo;
    }

    public List<FunctionVO> doToVos(List<Function> functionList) {
        if (functionList == null || functionList.isEmpty()) {
            return null;
        }
        List<FunctionVO> result = new ArrayList<>();
        for (Function function : functionList) {
            result.add(doToVo(function));
        }
        return result;
    }
}
